package com.lottery.im.pop;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.util.RelativePopupWindow;
import java.util.List;

/**
 * Created by lianwufeng on 2017/9/28.
 *
 * 会话 popup
 */

public class ConversationMenuPopup extends RelativePopupWindow {

  private Context mContext;

  private RecyclerView mListView;
  private RelativeLayout mLayoutAll;
  private List<String> mItems;

  public ConversationMenuPopup(Context context) {

    this.mContext = context;

    setContentView(getContentView(context));
    setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    setFocusable(true);
    setOutsideTouchable(true);
    setBackgroundDrawable(new BitmapDrawable());

    // Disable default animation for circular reveal
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      setAnimationStyle(0);
    }
  }

  private View getContentView(Context context) {

    View contentView =
        LayoutInflater.from(context).inflate(R.layout.pop_con_menu_list, null);

    mLayoutAll = contentView.findViewById(R.id.layout_all);
    mLayoutAll.setOnClickListener(v -> dismiss());

    mListView = contentView.findViewById(R.id.list);

    mListView.setLayoutManager(new LinearLayoutManager(context));
    mListView.setHasFixedSize(true);

    RecyclerViewDivider.with(context)
        .color(context.getResources().getColor(R.color.line_color))
        .size((int) context.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider()
        .build()
        .addTo(mListView);

    return contentView;
  }

  public void setDataSelect(List<String> item,
      final BaseQuickAdapter.OnItemClickListener onItemClickListener) {

    mItems = item;
    ItemAdapter adapter = new ItemAdapter(mItems);
    adapter.setOnItemClickListener((adapter1, view, position) -> {
      onItemClickListener.onItemClick(adapter1, view, position);
      dismiss();
    });
    mListView.setAdapter(adapter);
  }

  @Override
  public void showOnAnchor(@NonNull View anchor, @VerticalPosition int vertPos,
      @HorizontalPosition int horizPos) {
    super.showOnAnchor(anchor, vertPos, horizPos);
    Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.popup_from_top);
    mLayoutAll.startAnimation(anim);
  }

  private class ItemAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    public ItemAdapter(@Nullable List<String> data) {
      super(R.layout.item_menu_single_text, data);
    }

    @Override protected void convert(BaseViewHolder helper, String item) {
      helper.setText(R.id.tv_title, item);
    }
  }
}